package com.hualan.servlet;

import com.alibaba.fastjson.JSONObject;
import com.hualan.dao.ShopGoodsDao;
import com.hualan.dao.impl.ShopGoodsDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


@WebServlet("/change")
public class ChangeStatus extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if ("POST".equalsIgnoreCase(req.getMethod())) {
            System.out.println("Request Body:");
            BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream()));
            String line;
            StringBuilder content = new StringBuilder();
            while ((line = reader.readLine())!= null) {
                content.append(line);
            }
            String json =content.toString();
            JSONObject jsonObject = JSONObject.parseObject(json);
            int goodsId = jsonObject.getInteger("goods_id");
            int goodsStatus = jsonObject.getInteger("goods_status");
            ShopGoodsDao shopGoodsDao = new ShopGoodsDaoImpl();
            System.out.println(goodsStatus+"changestatus");
            shopGoodsDao.update(goodsId,goodsStatus);

        }
    }
}
